/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ai;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.LinkedList;

/**
 *
 * @author mammad
 */
public class KeyWordAI {

    private QueryForm f;
    private Jdbc jdbc;
    private LinkedList<String> ingr;
    private LinkedList<String> mealCat;
    private LinkedList<String> meals;
    private Connection mCon;
    ArrayList<String> incIngr;
    ArrayList<String> excIngr;
    ArrayList<String> incMealcat;
    ArrayList<String> excMealcat;
    ArrayList<String> incMeals;
    ArrayList<String> excMeals;

    public KeyWordAI(QueryForm form, Connection conn) {

        f = form;
        jdbc = new Jdbc(f);
        jdbc.getDatabaseIngr(conn);
        jdbc.getDatabaseMealCat(conn);
        jdbc.getDatabaseMeals(conn);
        ingr = f.getDatabaseIngr();
        mealCat = f.getDatabaseMealCat();
        meals = f.getDatabaseMeals();
        incIngr = new ArrayList<String>();
        excIngr = new ArrayList<String>();
        incMealcat = new ArrayList<String>();
        excMealcat = new ArrayList<String>();
        incMeals = new ArrayList<String>();
        excMeals = new ArrayList<String>();
        mCon = conn;

    }

    public void collectUserIncIngr(ArrayList<String> userInput) {

        ArrayList<String> words = userInput;
        if (words.contains("or")) {

            for (int j = 0; j < words.size(); j++) {
                for (int k = 0; k < ingr.size(); k++) {
                    if (words.get(j).equals(ingr.get(k))) {
                        f.addIncIngredient(words.get(j));
                    }
                }
            }

            if (!f.getIncIngrediens().isEmpty()) {
                jdbc.checkOrIncIngrediens(mCon);
            }

        } else if (words.contains("and") || words.contains("both")) {
            for (int j = 0; j < words.size(); j++) {
                for (int k = 0; k < ingr.size(); k++) {
                    if (words.get(j).equals(ingr.get(k))) {
                        f.addIncIngredient(words.get(j));
                    }
                }
            }
            if (!f.getIncIngrediens().isEmpty()) {
                jdbc.checkAndIncIngrediens(mCon);
            }
        } else {
            for (int j = 0; j < words.size(); j++) {
                for (int k = 0; k < ingr.size(); k++) {
                    if (words.get(j).equals(ingr.get(k))) {
                        f.addIncIngredient(words.get(j));
                    }
                }
            }
            if (!f.getIncIngrediens().isEmpty()) {
                jdbc.checkAndIncIngrediens(mCon);
            }
        }
    }

    public void collectUserExcIngr(ArrayList<String> userInput) {

        ArrayList<String> words = userInput;
        if (words.contains("or")) {
            for (int j = 0; j < words.size(); j++) {
                for (int k = 0; k < ingr.size(); k++) {
                    if (words.get(j).equals(ingr.get(k))) {
                        f.addExcIngredient(words.get(j));
                    }
                }
            }
            if (!f.getExcIngrediens().isEmpty()) {
                jdbc.checkOrExcIngrediens(mCon);
            }



        } else if (words.contains("and")) {
            for (int j = 0; j < words.size(); j++) {
                for (int k = 0; k < ingr.size(); k++) {
                    if (words.get(j).equals(ingr.get(k))) {
                        f.addExcIngredient(words.get(j));
                    }
                }
            }
            if (!f.getExcIngrediens().isEmpty()) {
                jdbc.checkAndExcIngrediens(mCon);
            }
        } else {
            for (int j = 0; j < words.size(); j++) {
                for (int k = 0; k < ingr.size(); k++) {
                    if (words.get(j).equals(ingr.get(k))) {
                        f.addExcIngredient(words.get(j));
                    }
                }
            }
            if (!f.getExcIngrediens().isEmpty()) {
                jdbc.checkAndExcIngrediens(mCon);
            }
        }
    }

    public void collectUserExcMealCat(ArrayList<String> userInput) {
        ArrayList<String> words = userInput;
        for (int j = 0; j < words.size(); j++) {
            for (int k = 0; k < mealCat.size(); k++) {
                if (words.get(j).equals(mealCat.get(k))) {
                    //System.out.println("mealCat " + words.get(j));
                    f.addExcMealCategory(words.get(j));
                }
            }
        }
        if (!f.getExcMealCategory().isEmpty()) {

            jdbc.checkExcMealCategories(mCon);
        }
    }

    public void collectUserIncMealCat(ArrayList<String> userInput) {
        ArrayList<String> words = userInput;
        for (int j = 0; j < words.size(); j++) {
            for (int k = 0; k < mealCat.size(); k++) {
                if (words.get(j).equals(mealCat.get(k))) {
                    //System.out.println("incMealCat " + words.get(j));
                    f.addIncMealCategory(words.get(j));
                }
            }
        }
        if (!f.getIncMealCategory().isEmpty()) {
            jdbc.checkIncMealCategories(mCon);
        }
    }

    public void collectUserExcMeals(ArrayList<String> userInput) {
        ArrayList<String> words = userInput;
        for (int j = 0; j < words.size(); j++) {
            for (int k = 0; k < meals.size(); k++) {
                if (words.get(j).equals(meals.get(k))) {
                    //System.out.println("incMealCat " + words.get(j));
                    f.addExcMeal(words.get(j));
                }
            }
        }
        if (!f.getExcMeal().isEmpty()) {
            jdbc.checkExcMeals(mCon);
        }
    }

    public void collectUserIncMeals(ArrayList<String> userInput) {
        ArrayList<String> words = userInput;
        for (int j = 0; j < words.size(); j++) {
            for (int k = 0; k < meals.size(); k++) {
                if (words.get(j).equals(meals.get(k))) {
                    //System.out.println("incMealCat " + words.get(j));
                    f.addIncMeal(words.get(j));
                }
            }
        }
        if (!f.getIncMeal().isEmpty()) {
            jdbc.checkIncMeals(mCon);
        }
    }

    public boolean inputHandler(String userInput) {

        f.setWasChanged(false);

        String[] parts = userInput.split(" but ");
        for (int i = 0; i < parts.length; i++) {
            internalInputHandler(parts[i].trim());
        }

        return f.getWasChanged();
    }

    private void internalInputHandler(String userInput) {
        //System.out.println("internalInputHandler start: incIngr " + incIngr + " excIngr " + excIngr );
        String[] w = userInput.split("\\s");
        if (containsWord(w, "no") || containsWord(w, "not")) {
            for (int i = 0; i < w.length; i++) {
                excIngr.add(w[i]);
                excMealcat.add(w[i]);
                excMeals.add(w[i]);
            }
        } else {
            for (int i = 0; i < w.length; i++) {
                incIngr.add(w[i]);
                incMealcat.add(w[i]);
                incMeals.add(w[i]);
            }
        }
        if (!incIngr.isEmpty()) {
            collectUserIncIngr(incIngr);
        }
        if (!excIngr.isEmpty()) {
            collectUserExcIngr(excIngr);
        }
        if (!f.getIncIngrediens().isEmpty() && !f.getExcIngrediens().isEmpty()) {
            jdbc.checkAndIncAndExcIngrediens(mCon);
        }
        if (!incMealcat.isEmpty()) {
            collectUserIncMealCat(incMealcat);
        }
        if (!excMealcat.isEmpty()) {
            collectUserExcMealCat(excMealcat);
        }
        if (!excMeals.isEmpty()) {
            collectUserExcMeals(excMeals);
        }
        if (!incMeals.isEmpty()) {
            collectUserIncMeals(incMeals);
        }
        
        //System.out.println("internalInputHandler end: incIngr " + incIngr + " excIngr " + excIngr );
        incIngr.clear();
        excIngr.clear();
        incMealcat.clear();
        excMealcat.clear();
        incMeals.clear();
        excMeals.clear();

    }

    private boolean containsWord(String[] words, String word) {
        for (int i = 0; i < words.length; i++) {
            if (words[i].equals(word)) {
                return true;
            }
        }
        return false;
    }
    }
    

    
        
    

   




